import { creatProjectResponseData } from "./vo"
import { queryGroupResponseData } from "./vo"
import { queryListResponseData  } from "./vo"
import ajax from "../../../modules/ajax";
import config from "../../../config.json"


export async function deleteGroupList(deleteId:number) : Promise<creatProjectResponseData>{
    try{
        let res = await ajax.ajax(config.requestUrl+"/gapi/group/delete", {
            method:"POST",
            headers: {
                "Content-Type" : "application/json",
            },
            body:JSON.stringify({
                groupId:deleteId,
            })
        })
        if(!res.ok){
            return Promise.reject("net error");
        }else{
            return res.json();
        }
    }catch(err:any){
        console.log(err);
    }
    return Promise.reject("unkonw error");
}


export async function requireListData(queryId:number) : Promise<queryListResponseData>{
    try{
        let res = await ajax.ajax(config.requestUrl+"/gapi/project/getList",{
            method:"GET",
            body:{
                groupId:queryId,
            }
        })
        if(!res.ok){
            return Promise.reject("net error");
        }else{
            return res.json();
        }
    }catch(err:any){
        console.log(err);
    }
    return Promise.reject("unkonw errow");
}

export async function requireGroupData(): Promise<queryGroupResponseData> {
    try {
        let res = await ajax.ajax(config.requestUrl+"/gapi/group/getList", {
            method: "GET",
        })
        if (!res.ok) {
            return Promise.reject("net error");
        }
        return res.json();
    } catch (err: any) {
        console.log(err);
    }
    return Promise.reject("unkonw error");
}

export async function SendReq(values: any): Promise<creatProjectResponseData> {
    try {
        let res = await ajax.ajax(config.requestUrl+"/gapi/project/create", {
            method: "POST",
            headers: {
                "Content-Type" : "application/json",
            },
            body: JSON.stringify({
                projectName: values.projectName,
                groupId: values.groupId,
                projectPath: values.projectPath,
                description: values.description,
                cookie: values.cookie,
                token: values.token,
                header: values.header,
            })

        });
        if (!res.ok) {
            return Promise.reject("net error");
        }
        return res.json();
    } catch (err: any) {
        console.log(err);
    }
    return Promise.reject("unkonw errow");
}

export async function SendReqAddFolder(values: any): Promise<creatProjectResponseData> {
    try {
        let res = await ajax.ajax(config.requestUrl+"/gapi/group/create", {
            method: "POST",
            headers: {
                "Content-Type": "application/json",
            },
            body: JSON.stringify({
                groupName: values.groupName,
            })
        });
        if (!res.ok) {
            return Promise.reject("net error");
        }
        return res.json();
    } catch (err: any) {
        console.log(err);
    }
    return Promise.reject("unkonw errow");
}